package controllers;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import database.dao.Impl.CustomerDaoImpl;
import entity.Customer;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import utils.MD5Util;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @ClassName RegisterViewController
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/15 15:49
 */
public class RegisterViewController implements Initializable {
    
    public TextField userNameField;
    public TextField phoneField;
    public JFXComboBox sexComboBox;
    public JFXButton registerBtn;
    public TextField addressField;
    public AnchorPane registerPane;
    public Button closeBtn;
    public PasswordField passwordField;

    private double xOffset;
    private double yOffset;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Stage stage= ViewManager.STAGE.get("registerStage");
        ViewManager.CONTROLLER.put("RegisterViewController",this);
        sexComboBox.getItems().addAll(new Label("男"),new Label("女"));

        //拖放功能
        registerPane.setOnMousePressed(event -> {
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
            registerPane.setCursor(Cursor.CLOSED_HAND);
        });

        registerPane.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });
        registerPane.setOnMouseReleased(event -> {
            registerPane.setCursor(Cursor.DEFAULT);
        });
    }

    /***
     * @Title:PostRegisterAct
     * @Description:注册操作
     * @param actionEvent
     * @return void
     * @Author: lzq
     */
    public void PostRegisterAct(ActionEvent actionEvent) {

        String password=passwordField.getText();
        String userName=userNameField.getText();
        String user_sex ="null";

        if(sexComboBox.getSelectionModel()!=null)
            user_sex=((Label) sexComboBox.getSelectionModel().getSelectedItem()).getText();

        String address=addressField.getText();
        String Phone=phoneField.getText();
        Integer customer_id;
        if(!userName.equals("") && !password.equals("") && !Phone.equals("")&&!address.equals("")&&!user_sex.equals("null")){
            Customer customer=new Customer(userName, MD5Util.MD5(password),Phone,address,user_sex);
            CustomerDaoImpl customerDao=new CustomerDaoImpl();

            customer_id =customerDao.addCustomer(customer);

            LoginViewController controller= (LoginViewController) ViewManager.CONTROLLER.get("LoginViewController");
            controller.userIdField.setText(customer_id.toString());
            controller.passwordField.setText(password);

            closeView();
        }else{
            Platform.runLater(()-> {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("Warning!");
                alert.setHeaderText("注册信息未填写完整");
                alert.setContentText("请确保信息正确填写完成，然后再进行注册");
                alert.showAndWait();
            });
        }


    }

    /***
     * @Title:closeAction
     * @Description:关闭注册窗口，取消StageManager对其窗口的管理
     * @param actionEvent
     * @return void
     * @Author: lzq
     */
    public void closeAction(ActionEvent actionEvent) {
        closeView();
    }

    public void  closeView(){
        Stage registerStage= ViewManager.STAGE.get("registerStage");
        ViewManager.STAGE.remove("registerStage");
        ViewManager.CONTROLLER.remove("RegisterViewController");
        registerStage.close();
    }

}
